<?php
/**
 * Created by PhpStorm.
 * User: 24369
 * Date: 2021/6/24
 * Time: 14:58
 */
namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\logic\GoodLogic;
use app\admin\logic\VipLogic;
use app\admin\model\User;
use think\facade\Request;
use think\facade\View;

class Vip extends BaseController
{
    public function vipList()
    {
        //搜索条件
        $where=VipLogic::getWhere();
        //关键字
        $keywords=Request::param('keywords');
        //展示会员
        $list=User::getUserList($where,$keywords);
        View::assign('list',$list);
        return  view();
    }
    public function vipAdd()
    {
        if (Request::isPost()){
            //验证上传数据
            list($msg,$post)=VipLogic::getVipDate();
            if (isset($msg)&&$msg!='')      $this->error($msg);
            //密码md5
            if (isset($post['pwd'])&&$post['pwd']!='')      $post['pwd']=md5($post['pwd']);
            //图片上传
            list($msg,$path)=GoodLogic::getImg('img','UploadFiles/product');
            if (isset($msg)&&$msg!='')      $this->error($msg);
            if (isset($path)&&$path!='')    $post['head_img']=$path;
            //添加时间
            $post['addtime']=time();
            //会员添加
            $res=User::create($post);
            if ($res){
                $this->success('添加成功',url('Vip/vipList'));
            }else{
                $this->error('添加失败',url('Vip/vipList'));
            }
        }else{
            return  view();
        }
    }
    //会员修改
    public function vipEdit(){
        $id=Request::param('id');
        if (Request::isPost()) {
            //表单数据验证
            list($msg,$post)=VipLogic::getVipDate();
            if (isset($msg)&&$msg!='')      $this->error($msg);
            //密码md5
            if (isset($post['pwd'])&&$post['pwd']!='')      $post['pwd']=md5($post['pwd']);
            //图片上传
            list($msg,$path)=GoodLogic::getImg('img','UploadFiles//product');
            if (isset($msg)&&$msg!='')      $this->error($msg);
            if (isset($path)&&$path!='')    $post['head_img']=$path;
            //添加修改id
            $post['id']=$id;
            $res=User::update($post);
            if ($res){
                $this->success('修改成功',url('Vip/vipList'));
            }else{
                $this->error('修改失败');
            }
        }else{
            if ($id){
                $vip=User::field(true)->find($id)->toArray();
                View::assign($vip);
            }
            return view();
        }
    }
    //会员列表软删除
    public function vipSoftDelete()
    {
        //获取要删除数据的id
        $id=Request::param('id');
        if ($id){
            //会员删除
            $del=User::destroy($id);
            if ($del){
                $this->success('删除成功',url('Vip/vipList'));
            }else{
                $this->error('删除失败',url('Vip/vipList'));
            }
        }
    }

}